# Title: ### AI name ###
# Author: ### LX ###
# Version: ### 1.1.0 ###
# Date: ### MM/DD/YYYY ###

import game.tcturtle
import random
import math

class CombatTurtle(game.tcturtle.TurtleParent):
    """Template Combat Turtle class.

    You may replace this docstring with any documentation you wish to include
    with this Combat Turtle AI, such as the general strategy it uses.
    """

    #-------------------------------------------------------------------------

    def class_name():
        """CombatTurtle.class_name() -> str
        Static method to return the name of the Combat Turtle AI.

        The returned name will be used in the AI selection menu by the main
        game driver, and in the game window to label each player.
        """

        ### Replace the returned string with the turtle's name.

        return "templateyouqiubiying"

    #-------------------------------------------------------------------------

    def class_desc():
        """CombatTurtle.class_desc() -> str
        Static method to return a description of the Combat Turtle AI.

        This should be a one-line description. The returned string will be
        used in the AI selection menu by the main game driver.
        """

        ### Replace the returned string with a one-line description.

        return "NB."

    #-------------------------------------------------------------------------

    def class_shape():
        """CombatTurtle.class_shape() -> (int or tuple)
        Static method to define the Combat Turtle's shape image.

        The return value can be either an integer or a tuple of tuples.

        Returning an integer index selects one of the following preset shapes:
            0 -- arrowhead (also default in case of unrecognized index)
            1 -- turtle
            2 -- plow
            3 -- triangle
            4 -- kite
            5 -- pentagon
            6 -- hexagon
            7 -- star

        A custom shape can be defined by returning a tuple of the form
        (radius, angle), where radius is a tuple of radii and angle is a tuple
        of angles (in radians) describing the polar coordinates of a polygon's
        vertices. The shape coordinates should be given for a turtle facing
        east.
        """

        ### If desired, replace the returned value with a different shape
        ### index (see guide above) or a 2-tuple of n-tuples describing the
        ### radii and angles (respectively) of the turtle's polar shape
        ### coordinates (relative to a turtle facing east).

        ### The following is an example of an acceptable set of custom
        ### coordinates:
        ### return ((16, 14, 12, 14), (0, math.pi/2, math.pi, 3*math.pi/2))

        return 0

    #=========================================================================

    def setup(self):
        """CombatTurtle.setup() -> None
        Initialization code for Combat Turtle.
        """

        # Initialize goal coordinate to move towards  初始化要移动的目标坐标
        self.goal = self.update_goal()

        (self.head, self.wait) = self.rerandomize()
        # Set distance within which to shoot instead of mirror 设置射击距离
        self.shoot_dist = 0.5* self.missile_range
        #
        # self.nose_rel = (8, 0.0)  # just ahead of turtle's front
        # self.hand_rel = (8, math.pi / 2)  # to left of turtle




    #-------------------------------------------------------------------------

    def step(self):
        """CombatTurtle.setup() -> None
        Step event code for Combat Turtle.
        """
        # Determine behavior based on whether there is line of sight and the
        # opponent is close enough to shoot at
        # 根据是否有视线和对手是否足够靠近来决定行为
        i = 1
        if self.line_of_sight() and self.distance() <= self.shoot_dist:
            # If there is a clear shot, turn towards opponent and shoot
            # 可以射击
            # Turn towards opponent

            self.turn_towards()
            self.forward()
            if self.can_shoot:
                self.shoot()
            if self.distance() < 4 * self.missile_radius:
                if self.can_shoot:
                    self.shoot()
                self.stop_tui()
        elif self.line_of_sight() and self.distance() <= 2*self.shoot_dist:
            self.turn_towards(self.head)
            self.forward()
            if self.can_shoot:
                self.shoot()
        else:
            # If there is no clear shot, attempt to move to goal coordinate
            #
            # Update target coordinate
            self.goal = self.update_goal()

            # Turn towards goal
            self.turn_towards(self.goal)

            # Calculate fraction of speed that will take us as close as
            # possible to the goal in one step of movement
            spd = max(1, self.distance(self.goal) / self.max_speed)


            self.forward(spd)

    def update_goal(self):
        """MirrorTurtle.update_goal() -> tuple
        Determines the target coordinate that this turtle should move towards.

        During each step this turtle calculates the point within the arena
        opposite the opponent's position (rotated around the central axis).
        """
        # 与对手位置相反
        return (self.arena_right - self.other_x,
                self.arena_top - self.other_y)

    def rerandomize(self):
        """WandererTurtle.rerandomize() -> tuple
        Resets the randomly-chosen heading and timer cutoff.

        The Wanderer turtle AI wanders randomly by periodically choosing a
        random direction to move in. This method chooses a new random heading
        and randomizes a timer which determines when to re-randomize the
        heading.

        Returns a tuple which includes the new heading (int) and the new
        number of steps to wait (int).
        """

        rh = random.randrange(-179, 181)  # random heading
        rt = random.randrange(5, 31)  # random timer

        return (rh, rt)
    #后退
    def stop_tui(self):
        self.turn_towards(self.head)
        if self.distance() <= self.shoot_dist:
            self.backward()
            if self.speed == 0 :
                self.turn_towards(self.head)
                self.forward()





